package cn.yogehaoren.dlx;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author WangNing
 * @since 1.0
 */
public class DLXProducer {

    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("k8s-node2");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");

        Connection connection = connectionFactory.newConnection();

        Channel channel = connection.createChannel();

        // 指定消息的确认模式
        channel.confirmSelect();

        // 设置过期时间，测试死信队列
        AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder()
                .deliveryMode(2)
                .expiration("10000")
                .contentEncoding("UTF-8")
                .build();

        String exchangeName = "test_dlx_exchange";
        String routingKey = "dlx.save";
        for(int i=0;i<10;i++){
            String msg = "Hello RabbitMQ send DLX message From " + i;
            channel.basicPublish(exchangeName, routingKey, properties, msg.getBytes());
        }


        // 关闭连接，线程会退出，否则不会主动退出
        channel.close();
        connection.close();

    }


}
